Ext.onReady(function(){
	this.exam_design_problem=new Array();
	this.auto_increment=0;
	Ext.QuickTips.init();
	var singleSelectCount=0;
	var RESULT={};
	var bar = new Ext.Toolbar([{
						text : "单项选择题",
						icon : '../../resource/upload.png',
						handler:addSingleSelect
					},{
						text : "填空题",
						icon : '../../resource/upload.png',
						handler:addBlankExam
					}]);
	var exam_panel=new Ext.Panel({
		applyTo:'exam_div',
		tbar:bar
	});
	var mainDom=exam_panel.getEl().dom;
	var node=mainDom.children[0].children[1];
	var exam_container=document.createElement("div");
	exam_container.setAttribute("id","div_exam_container");
	node.appendChild(exam_container);
	node.setAttribute("style","background-color: #D0DEF0;");
	new Ext.Viewport({
		layout:'fit',
		frame:false,
		items:[
			exam_panel
		]
	});
	var singleSelectPanel=new Ext.Panel({
		layout:'form',
		labelAlign:'right',
		border:false,
		frame:true,
		buttonAlign:'center',
		id:'singleSelectPanel',
		name:'singleSelectPanel',
		buttons:[
		new Ext.Button({
			text:'添加选项',
			handler:function(){
				++singleSelectCount;
				var n=createSingleSelection(singleSelectCount);
				singleSelectPanel.add(n);
				singleSelectPanel.doLayout();
			}
		}),
		new Ext.Button({
			text:'确定',
			handler:function(){
			}
		})
	]
	});
	singleSelectPanel.newsingle={};
	var blankExamPanel=new Ext.Panel({
		layout:'form',
		labelAlign:'right',
		border:false,
		frame:true,
		buttonAlign:'center',
		id:'blankExamPanel',
		name:'blankExamPanel',
		items:[{
			xtype:'textarea',
			id:'blank_exam_content',
			fieldLabel:'题目', 
            anchor:'95%' 
		},{
			xtype:'box',
			id:'blank_exam_format',
			style:"margin-left:100px"
		}],
		buttons:[
		new Ext.Button({
			text:'生成空格',
			handler:function(){
				var content=Ext.getDom('blank_exam_content').value;
				content=content.replace(/\?/g,'<input name="blank_input" type="text" style="width:50px;margin:0px 3px 3px 3px"/>');
				var box=Ext.getDom('blank_exam_format');
				box.innerHTML=content;
			}
		}),
		new Ext.Button({
			text:'确定',
			handler:function(){
				
			}
		})]
	});
	var singleSelectWin=new Ext.Window({
		plain:true,
		width:500,
		closeAction:'hide',
		bodyStyle:'padding:5px',
		modal:true,
		closable:true,
		animCollapse:true,
		resizable:false,
		items:[singleSelectPanel
		],
		listeners:
		{
			'beforehide':function(){addSingleSelectionElement();}
		}
	});
	var blankExamWin=new Ext.Window({
		plain:true,
		width:500,
		closeAction:'hide',
		bodyStyle:'padding:5px',
		modal:true,
		closable:true,
		animCollapse:true,
		resizable:false,
		items:[
			blankExamPanel
		],
		listeners:{
			'beforehide':function(){
				addBlankExamElement();
			}
		}
	});
	function addSingleSelect()
	{
		singleSelectCount=4;
		var r1=new Ext.FormPanel({
				labelAlign:'right',
				border:false,
				items:[{
					id:'single_describer',
					xtype:'textarea',
					fieldLabel:'题目说明', 
                    anchor:'95%' 
				}]
			});
		singleSelectPanel.removeAll();
		singleSelectPanel.add(r1);
		for(var i=1;i<=4;++i)
			singleSelectPanel.add(createSingleSelection(i));
		singleSelectPanel.doLayout();
		singleSelectWin.setTitle('单项选择题');
		singleSelectWin.show();
	}
	function createSingleSelection(id)
	{
		var config = {
					xtype : 'compositefield',
					fieldLabel : '选项'+id,
					id:'single_'+id,
					msgTarget : 'qtip',
					items : [{
								xtype : 'textfield',
								id : 'single_answer'+id,
								width : 200
							}, {
								xtype : 'button',
								text : '删除',
								width : 29,
								margins : '0 5 0 0',
								handler:function(){
									var cm=singleSelectPanel.findById('single_'+id);
									singleSelectPanel.remove(cm,true);
								}
							}, {
								xtype : 'button',
								text : '设为正确答案',
								width : 48,
								margins : '0 35 0 0',
								handler:function(){
									for(var i=1;i<=singleSelectCount;++i)
									{
										var box=Ext.getCmp('marke_img_'+i);
										if(box)
											box.setVisible(false);
											
									}
									Ext.getCmp('marke_img_'+id).show();
									singleSelectPanel.doLayout();
								}
							},{
								xtype : 'box',
								margins : '3 0 0 0',
								id:'marke_img_'+id,
								hidden:true,
								autoEl: {
            						tag: 'img',
           							src: '../../resource/yes.gif'
        							}
							}]
				};
		return config;
	};
	function addBlankExam()
	{
		blankExamWin.setTitle('填空题');
		blankExamWin.show();
	}
	function addSingleSelectionElement(ob)
	{
		var newsingle = {};
		newsingle.type = "single";
		newsingle.describer = Ext.get('single_describer').dom.value;
		newsingle.items = new Array();
		for (var i = 1; i <= singleSelectCount; ++i) {
			var com = Ext.get('single_answer' + i);
			if (com) {
				if (Ext.getCmp('marke_img_' + i).hidden == false)
					newsingle.answer = newsingle.items.length;
				newsingle.items[newsingle.items.length] = com.dom.value;
			}
		}
		this.exam_design_problem[this.exam_design_problem.length] = newsingle;	
		var div_discriber=document.createElement("div");	//创建问题描述div
		div_discriber.setAttribute("class","single_describer");
		div_discriber.innerHTML=newsingle.describer;
		var ul=document.createElement("ul");
		for(var i=0;i<newsingle.items.length;++i)
		{
			var li=document.createElement("li");
			var radio=document.createElement("input");
			radio.setAttribute("type","radio");
			radio.setAttribute("name","q"+this.auto_increment);
			var label=document.createElement("label");
			label.setAttribute("for","q"+this.auto_increment);
			label.innerHTML=newsingle.items[i];
			li.appendChild(radio);
			li.appendChild(label);
			ul.appendChild(li);
		}
		var div_single=document.createElement("div");
		div_single.appendChild(div_discriber);
		div_single.appendChild(ul);
		addQuestion(div_single);
		
	}
	function showborder(ele)
	{
		//ele.setAttribute("class","examdiv");
		ele.setAttribute("style","border-style:solid;padding:0px;");
	}
	function hideborder(ele)
	{
		ele.setAttribute("style","border-style:node;padding:2px;");
	}
	function addBlankExamElement()
	{
		var newblank={};
		newblank.type="blank";
		newblank.describer=Ext.getDom('blank_exam_content').value;
		newblank.answers=new Array();
		var dom=Ext.getDom('blank_exam_format');
		var inputlist=dom.getElementsByTagName('input');
		for(var i=0;i<inputlist.length;++i)
		{
			newblank.answers[i]=inputlist[i].value;
		}
		this.exam_design_problem[this.exam_design_problem.length] = newblank;
		//格式化字符串
		var index=0;
		var curindex = -1;
		var preindex = 0;
		var newstring = "";
		var str=newblank.describer;
		do {
			preindex = curindex;
			curindex = str.indexOf("?", curindex + 1);
			if (curindex != -1 && (curindex == 0 || str[curindex - 1] != '\\')) {
				newstring += str.substring(preindex + 1, curindex);
				newstring += newblank.answers[index];
				++index;
			}
		} while (curindex != -1);
		newstring += str.substring(preindex+1, str.length);

		var div_blank = document.createElement("div"); //创建问题描述div
		div_blank.setAttribute("class","blank_descreber");
		div_blank.innerHTML=newstring;
		addQuestion(div_blank);
	}
	function addQuestion(div_question)
	{
		var node=document.getElementById("div_exam_container");
		var div=document.createElement("div");
		var div_op=document.createElement("div");
		div_op.setAttribute("class","div_operation");
		var ul=document.createElement("ul");
		ul.appendChild(createOp("delete_icon","删除",deleteQuestion,div,"删除该题目"));
		ul.appendChild(createOp("edit_icon","编辑",deleteQuestion,div,"编辑该题目"));
		div_op.appendChild(ul);
		div.setAttribute("id","problem_"+this.auto_increment);
		div.setAttribute("class","examdiv");
		div.onmousemove = function() {
			if(ul.getAttribute("visibility")=='visible')
				return;
			ul.setAttribute("style", "visibility:visible");
			showborder(this);
		};
		div.onmouseout = function() {
			if(ul.getAttribute("visibility")=='hidden')
				return;
			ul.setAttribute("style", "visibility:hidden");
			hideborder(this);
		};
		div.appendChild(div_question);
		div.appendChild(div_op);
		node.appendChild(div);
		this.auto_increment++;
	}
	function deleteQuestion(div)
	{
		div.parentElement.removeChild(div);
	}
	function createOp(iconcls,text,clickfun,parent,title)
	{
		var li=document.createElement("li");
		var a=document.createElement("a");
		a.setAttribute("href","javascript:void(0)");
		a.setAttribute("title",title);
		a.onclick=function(){clickfun(parent);};
		var icon=document.createElement("span");
		icon.setAttribute("class",'op_icon '+iconcls);
		var txt=document.createElement("span");
		txt.appendChild(document.createTextNode(text));
		a.appendChild(icon);
		a.appendChild(txt);
		li.appendChild(a);
		return li;
	}

});
